package at.ac.tuwien.mg.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import org.activiti.engine.ProcessEngine;

@ManagedBean
@SessionScoped
public class ManualOffers implements Serializable {

	@ManagedProperty(value="#{activiti.processEngine}")
	private ProcessEngine engine;
	
	@ManagedProperty(value="#{user}")
	private User user;
	
	private static final long serialVersionUID = 2177416270826700804L;

	private Vector<Offer> offers = new Vector<Offer>();
	private Offer toDelete;
	
	private String supplier;
	private int amount;
	private String good;
	private long price;	
	
	public ManualOffers() {
		amount = 10;
		price = 10l;
		good = "Gold";
	}
		
	public ProcessEngine getEngine() {
		return engine;
	}

	public void setEngine(ProcessEngine engine) {
		this.engine = engine;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public String getSupplier() {
		return supplier;
	}

	public void setSupplier(String s) {

		supplier = s;
		
	}

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}

	public String getGood() {
		return good;
	}

	public void setGood(String good) {
		this.good = good;
	}

	public long getPrice() {
		return price;
	}

	public void setPrice(long price) {
		this.price = price;
	}
	
	public Offer getToDelete() {
		return toDelete;
	}

	public void setToDelete(Offer toDelete) {
		this.toDelete = toDelete;
	}
	
	public Vector<Offer> getOffers() {
		return offers;
	}

	public void setOffers(Vector<Offer> offers) {
		this.offers = offers;
	}


	public String add() {
		
		List<Item> items = new ArrayList<Item>();
		Item i = new Item(good, amount, price);
		boolean found = false;

		Supplier foundSupplier = null;
		SupplierList sl = new SupplierList();			
		for(Supplier s : sl.getSupplier()) {
			if(s.getName().equals(supplier)) {
				foundSupplier = s;
			}
		}
				
		for(Offer o : offers) {
			if(o.getSupplier().getName().equals(foundSupplier.getName())) {
				o.getItems().add(i);
				found = true;
				break;
			}
		}
		
		if(!found) {
			items.add(i);
			Offer o = new Offer(foundSupplier, items);
			offers.add(o);
		}
		
		return "";

	}
		
	public String remove() {
		
		offers.remove(toDelete);
		return "";
		
	}
	
	public String done() {
		
		// Add manual offers to other offers.
		System.out.println("PIID " + user.getLastProcInstanceId());
		
		@SuppressWarnings("unchecked")
		Vector<Offer> currentOffers = (Vector<Offer>) engine.getRuntimeService().getVariable(user.getLastProcInstanceId(), "receivedOffers");
		if(currentOffers == null) {
			currentOffers = new Vector<Offer>();
		}
		
		for(Offer o: offers) {
			currentOffers.add(o);
		}
		engine.getRuntimeService().setVariable(user.getLastProcInstanceId(), "receivedOffers", (Object) currentOffers);
		
		// Counter has to be increased.
		Integer i = (Integer) engine.getRuntimeService().getVariable(user.getLastProcInstanceId(), "automaticRequests");
		i += offers.size();
		engine.getRuntimeService().setVariable(user.getLastProcInstanceId(), "automaticRequests", (Object) i);
		
		// Remove task from list of open tasks.
		return user.finishTask();
		
	}
}
